package com.google.coffeeshop.gui.desktop.orderpayment.invoiceexport;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import com.google.coffeeshop.business.OrderBusiness;
import com.google.coffeeshop.business.PaymentBusiness;
import com.google.coffeeshop.entity.InvoiceDTO;
import com.google.coffeeshop.entity.OrderDTO;
import com.google.coffeeshop.gui.desktop.SwingFactory;
import com.google.coffeeshop.gui.desktop.orderpayment.OrderPaymentPanel;
import com.google.coffeeshop.gui.desktop.orderpayment.SeatBoardPanel;
import com.google.coffeeshop.gui.desktop.orderpayment.seatdetail.SeatDetailFrame;

public class PaymentFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	private JTextField tfTotalAmount;
	private JTextField tfDiscountAmount;
	private JTextField tfPayAmount;
	private long seatId;
	private OrderPaymentPanel pnlOrderPayment;
	private SeatBoardPanel pnlSeatBoard;
	private SeatDetailFrame frmSeatDetail;
	
	public PaymentFrame(OrderPaymentPanel pnlOrderPayment, long seatId) {
		this.pnlOrderPayment = pnlOrderPayment;
		this.seatId = seatId;
		initGUI();
	}

	public PaymentFrame(SeatBoardPanel pnlSeatBoard, SeatDetailFrame frmSeatDetail, long seatId) {
		this.frmSeatDetail = frmSeatDetail;
		this.pnlSeatBoard = pnlSeatBoard;
		this.seatId = seatId;
		initGUI();
	}
	
	private void initGUI() {
		setTitle("Export Invoice");
		setSize(453, 255);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		getContentPane().setLayout(gridBagLayout);
		
		JLabel lblNewLabel = new JLabel("Export Invoice");
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
		GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
		gbc_lblNewLabel.gridwidth = 2;
		gbc_lblNewLabel.insets = new Insets(0, 0, 5, 0);
		gbc_lblNewLabel.gridx = 0;
		gbc_lblNewLabel.gridy = 0;
		getContentPane().add(lblNewLabel, gbc_lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("Total Fee");
		GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
		gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_1.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_1.gridx = 0;
		gbc_lblNewLabel_1.gridy = 1;
		getContentPane().add(lblNewLabel_1, gbc_lblNewLabel_1);
		
		tfTotalAmount = new JTextField();
		tfTotalAmount.setEditable(false);
		GridBagConstraints gbc_tfTotalAmount = new GridBagConstraints();
		gbc_tfTotalAmount.insets = new Insets(0, 0, 5, 0);
		gbc_tfTotalAmount.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfTotalAmount.gridx = 1;
		gbc_tfTotalAmount.gridy = 1;
		getContentPane().add(tfTotalAmount, gbc_tfTotalAmount);
		tfTotalAmount.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("Discount");
		GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
		gbc_lblNewLabel_2.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_2.gridx = 0;
		gbc_lblNewLabel_2.gridy = 2;
		getContentPane().add(lblNewLabel_2, gbc_lblNewLabel_2);
		
		tfDiscountAmount = new JTextField();
		tfDiscountAmount.setEditable(false);
		GridBagConstraints gbc_tfDiscountAmount = new GridBagConstraints();
		gbc_tfDiscountAmount.insets = new Insets(0, 0, 5, 0);
		gbc_tfDiscountAmount.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfDiscountAmount.gridx = 1;
		gbc_tfDiscountAmount.gridy = 2;
		getContentPane().add(tfDiscountAmount, gbc_tfDiscountAmount);
		tfDiscountAmount.setColumns(10);
		
		JLabel label = new JLabel("____________________________________________________________");
		GridBagConstraints gbc_label = new GridBagConstraints();
		gbc_label.anchor = GridBagConstraints.EAST;
		gbc_label.gridwidth = 2;
		gbc_label.insets = new Insets(0, 0, 5, 0);
		gbc_label.gridx = 0;
		gbc_label.gridy = 3;
		getContentPane().add(label, gbc_label);
		
		JLabel lblNewLabel_3 = new JLabel("Have to Pay");
		GridBagConstraints gbc_lblNewLabel_3 = new GridBagConstraints();
		gbc_lblNewLabel_3.anchor = GridBagConstraints.EAST;
		gbc_lblNewLabel_3.insets = new Insets(0, 0, 5, 5);
		gbc_lblNewLabel_3.gridx = 0;
		gbc_lblNewLabel_3.gridy = 4;
		getContentPane().add(lblNewLabel_3, gbc_lblNewLabel_3);
		
		tfPayAmount = new JTextField();
		tfPayAmount.setEditable(false);
		GridBagConstraints gbc_tfPayAmount = new GridBagConstraints();
		gbc_tfPayAmount.insets = new Insets(0, 0, 5, 0);
		gbc_tfPayAmount.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfPayAmount.gridx = 1;
		gbc_tfPayAmount.gridy = 4;
		getContentPane().add(tfPayAmount, gbc_tfPayAmount);
		tfPayAmount.setColumns(10);
		
		JButton btnExport = new JButton("Export Now");
		btnExport.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				exportInvoice();
			}
		});
		GridBagConstraints gbc_btnExport = new GridBagConstraints();
		gbc_btnExport.gridheight = 2;
		gbc_btnExport.fill = GridBagConstraints.BOTH;
		gbc_btnExport.gridwidth = 2;
		gbc_btnExport.gridx = 0;
		gbc_btnExport.gridy = 6;
		getContentPane().add(btnExport, gbc_btnExport);
		
		setData(seatId);
	}
	
	private void setData(long seatId) {
		OrderDTO order = PaymentBusiness.findOrderUnProcessedBySeatId(seatId);
		if (order != null) {
			tfTotalAmount.setText(String.valueOf(order.getAmount()));
			tfDiscountAmount.setText("0");
			tfPayAmount.setText(String.valueOf(order.getAmount()));
		}
	}
	
	private void exportInvoice() {
		OrderDTO order = OrderBusiness.findOrder(seatId);
		InvoiceDTO invoice = PaymentBusiness.exportInvoice(order);
		if (SwingFactory.showConfirmDialog("Exported! Have the customer accepted the invoice?")) {
			PaymentBusiness.completePayment(invoice.getId(), order.getId(), order.getSeat().getId());
			if (frmSeatDetail != null) {
				frmSeatDetail.disposeFrame();
			}
		}
		dispose();
		refreshParentGUI();
	}
	
	private void refreshParentGUI() {
		if (pnlOrderPayment != null) {
			pnlOrderPayment.refreshGUI();
		}
		if (pnlSeatBoard != null) {
			pnlSeatBoard.refreshGuiAndParent();
		}
	}
	
}
